package com.library.BookCata.controller;

import com.library.Book.model.Book;
import com.library.Book.model.BookExample;
import com.library.BookCata.model.BookCata;
import com.library.BookCata.model.BookCataExample;
import com.library.BookCata.service.BookCataService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
public class BookCataController {
    
    private BookCataService bookCataService;

    //通过构造器注入service
    @Autowired
    public BookCataController(BookCataService bookCataService) {
        this.bookCataService = bookCataService;
    }

    //查询数据
    @GetMapping("/bookcata")
    @ResponseBody
    public List<BookCata> list() {
        //1 构造查询条件
        //1.1 创建查询条件类
        BookCataExample example = new BookCataExample();
        //1.2 创建条件构造器
        BookCataExample.Criteria criteria = example.createCriteria();
        //1.3 通过criteria设置条件
        //...
        
        //2 根据条件查询，如果没有查询条件，传入一个空的example即可
        List<BookCata> list = bookCataService.selectByExample(example);
        
        //3 返回结果
        return list;
    }

    //添加数据
    @PostMapping("/bookcata")
    @ResponseBody
    public BookCata add(@RequestBody BookCata bookCata) {
        //1 准备数据，例如设置bookCata主键等
        //...
        bookCata.setId((int) (System.currentTimeMillis() % 1000000000));
        
        //2 插入数据
        bookCataService.insertSelective(bookCata);
        
        //3 把新添加的数据返回给调用者
        return bookCata;
    }

    //修改数据
    @PutMapping("/bookcata")
    @ResponseBody
    public BookCata update(@RequestBody BookCata bookCata) {
        //1 准备数据，例如设置bookCata的修改时间等
        //...
        
        //2 更新数据
        bookCataService.updateByPrimaryKeySelective(bookCata);
        
        //3 把新修改的数据返回给调用者
        return bookCataService.selectByPrimaryKey(bookCata.getId());
    }

    //删除数据
    @DeleteMapping("/bookcata/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") Integer id) {
        //1 根据主键删除数据
        bookCataService.deleteByPrimaryKey(id);
        
        //2 返回一个状态码给调用者
        return "200";
    }


    // 返回数量和出版社名
    @GetMapping("/publish-stats")
    @ResponseBody
    public List<Map<String, Object>> getPublishStats() {
        List<BookCata> allPublishes = bookCataService.selectByExample(new BookCataExample());
        Map<String, Integer> bookCounts = new HashMap<>();
        for (BookCata publish : allPublishes) {
            String publisher = publish.getPublish();
            if (publisher != null) {
                bookCounts.put(publisher, bookCounts.getOrDefault(publisher, 0) + 1);
            }
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : bookCounts.entrySet()) {
            Map<String, Object> publisherStats = new HashMap<>();
            publisherStats.put("name", entry.getKey());
            publisherStats.put("value", entry.getValue());
            result.add(publisherStats);
        }
        return result;
    }



    // 返回9个出版社信息，每条信息里是出版社名称和出版社图书的数量
    /*@GetMapping("/publish-stats")
    @ResponseBody
    public List<Map<String, Object>> getPublishStats(@RequestParam(value = "limit", defaultValue = "8") int limit) {
        List<Book> allPublishes = bookService.selectByExample(new BookExample());

        List<String> selectedPublishers = new ArrayList<>();
        while (selectedPublishers.size() < limit && selectedPublishers.size() < allPublishes.size()) {
            Book randomPublisher = getRandomPublisher(allPublishes);
            if (!selectedPublishers.contains(randomPublisher.getPublish())) {
                selectedPublishers.add(randomPublisher.getPublish());
            }
        }

        List<Map<String, Object>> result = new ArrayList<>();
        for (String publisher : selectedPublishers) {
            int bookCount = getBookCountByPublisher(publisher);
            Map<String, Object> publisherStats = new HashMap<>();
            publisherStats.put("name", publisher);
            publisherStats.put("value", bookCount);
            result.add(publisherStats);
        }
        return result;
    }

    private Book getRandomPublisher(List<Book> allPublishes) {
        Random random = new Random();
        int index = random.nextInt(allPublishes.size());
        return allPublishes.get(index);
    }

    private int getBookCountByPublisher(String publisher) {
        BookExample bookExample = new BookExample();
        bookExample.createCriteria().andPublishEqualTo(publisher);
        return (int) bookService.countByExample(bookExample);
    }*/

}